<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-09 19:13:27 +0200 (Mon, 09 Jun 2008) $
 * $Rev: 1142 $
 * $Author: gavinspearhead $
 * $Id: functions.php 1142 2008-06-09 17:13:27Z gavinspearhead $
 */

if (!defined('ORIGINAL_PAGE')) die('This file cannot be accessed directly.');


$pathmf = realpath(dirname(__FILE__));


require_once "$pathmf/defines.php";
require_once "$pathmf/urdversion.php";
require_once "$pathmf/autoincludes.php";


function send_mail(DatabaseConnection $db, $msg, $sender, $email, $subject)
{
	assert($email != '');
	$sendmail = get_config($db, 'sendmail');
	if (!$sendmail) // sending mail not allowed by admin
		return;
	$msg = wordwrap($msg, 70, "\r\n");
	$headers = "From: $sender" . "\r\n" .
    		"Reply-To: $sender" . "\r\n" .
    		'X-Mailer: URD/' . get_version();
	$rv = mail($email, $subject, $msg, $headers);
	if ($rv === FALSE)
		throw new exception ('Could not send message.', ERR_SEND_FAILED);
}


function mail_user_update(DatabaseConnection $db, $username, $fullname, $email, $isadmin, $active, $sender)
{
	assert($email != '');
	$url = get_config($db, 'url');
    if (substr($url, -1) != '/')
        $url .= '/';
    if ($active == USER_ACTIVE) 
        $msg = "Your account has been activated. Proceed to login at {$url}html/login.php.";
    else if ($active == USER_INACTIVE) 
        $msg = 'Your account has been disabled. Contact the admin for further information';
	else return; //  error
	$subject = 'Account modified';	
	$msg = <<<MSG
Hello $fullname,

$msg

The URD administrator

MSG;
	send_mail($db, $msg, $sender, $email, $subject);
}


function mail_admin_new_user(DatabaseConnection $db, $name, $fullname, $email_to, $sender)
{
	assert($email_to != '');
	$url = get_config($db, 'url');
	$subject = 'New URD user';
	$msg = <<<MSG
Hello Admin,

A new user has been added: 
Username : $name
Full name : $fullname

The URD administrator
MSG;
	
	send_mail($db, $msg, $sender, $email_to, $subject);
}


function mail_pw(DatabaseConnection $db, $name, $email, $password, $sender)
{
	assert($email != '');
	$url = get_config($db, 'url');
    if (substr($url, -1) != '/')
        $url .= '/';
	$subject = 'Your password has been reset';
	$msg = <<<MSG
Hello $name,

Your password for URD has been reset. You can now login with this password: $password
Click here $url to login.

The URD administrator
MSG;
	send_mail($db, $msg, $sender, $email, $subject);
}


function mail_activation(DatabaseConnection $db, $username, $name, $email, $token, $sender)
{
	assert($email != '');
	$url = get_config($db, 'url');

    if (substr($url, -1) != '/')
        $url .= '/';
	$subject = 'Please activate your account';
	$msg = <<<MSG
Hello $name,

Your account has been created. To activate it, click on this link:
{$url}html/register.php?activate=$token&username=$username.

The URD administrator
MSG;
	send_mail($db, $msg, $sender, $email, $subject);
}


function mail_update_preferences(DatabaseConnection $db, $userid)
{
	$mail_user = get_pref($db, 'mail_user', $userid);
	if ($mail_user == 0)
		return;
	$sender = get_config($db, 'admin_email');
	$qry = "\"email\", \"fullname\" FROM users WHERE \"ID\" = $userid";
	$res = $db->select_query($qry,1);
	if (isset($res[0]['email'])) {
		$email = $res[0]['email'];
		$name = $res[0]['fullname'];
	} else 
		throw new exception ('Could not find user', ERR_INVALID_USERNAME);
	$subject = 'URD preferences update';

    $url = get_config($db, 'url');
    if (substr($url, -1) != '/')
        $url .= '/';
	$msg = <<<MSG
Hello $name,

New preferences have been added. Please validate your settings in the preferences menu:
{$url}html/prefs.php

The URD administrator
MSG;
	send_mail($db, $msg, $sender, $email, $subject);
}


function mail_user_download(DatabaseConnection $db, $dlid, $username, $status)
{
	assert(is_numeric($dlid));
	$mail_user = get_pref($db, 'mail_user', $username);
	if ($mail_user == 0)
		return;
    $sender = get_config($db, 'admin_email');
    $dlname = get_download_name($db, $dlid);
	$db->escape($username, TRUE);
	$qry = "\"email\", \"fullname\" FROM users WHERE \"name\" = $username";
	$res = $db->select_query($qry,1);
	if (isset($res[0]['email'])) {
		$email = $res[0]['email'];
		$name = $res[0]['fullname'];
	} else 
		throw new exception ('Could not find user', ERR_INVALID_USERNAME);
	$subject = 'URD Download status update';
	switch ($status) {
	case DOWNLOAD_FINISHED :
		$notice = 'is complete.';
		break;
	case DOWNLOAD_RAR_FAILED:
		$notice = 'failed to unrar properly.';
		break;
	case DOWNLOAD_CKSFV_FAILED:
		$notice = 'failed the checksum test.';
		break;
	case DOWNLOAD_PAR_FAILED:
		$notice = 'could not be repaired by par2.';
		break;
	case DOWNLOAD_ERROR:
		$notice = 'failed to download correctly.';
		break;
	default:
		$notice = "is in an unknown state ($status). This should not happen.";
		break;
	}
    $url = get_config($db, 'url');
    if (substr($url, -1) != '/')
        $url .= '/';

	$msg = <<<MSG
Hello $name,

Your download '$dlname' $notice

See {$url}html/transfers.php for more info.

The URD administrator

MSG;

	send_mail($db, $msg, $sender, $email, $subject);
}


?>
